package van.planifolia.stream;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Intellij IDEA<br>
 *
 * @author Planifolia.Van
 * @version 1.0
 * @date 2022/11/4 17:58
 */
public class StreamDemo {
    public static void main(String[] args) {
        List<People> peoples = new ArrayList<>();
        peoples.add(new People("刘星",18,"米国"));
        peoples.add(new People("刘赞",16,"中国"));
        peoples.add(new People("王星",21,"俄国"));
        peoples.add(new People("赵星",46,"法国"));
        peoples.add(new People("刘参",33,"中国"));
        peoples.add(new People("刘忙",19,"霓虹国"));
        peoples.add(new People("刘参宿",24,"中国"));
        stream(peoples);
    }
    /**
     * 使用Stream流式计算集合
     * @param peoples 被计算的集合
     */
    public static void stream(List<People> peoples){
        peoples.stream()
                // 找到大于18的
                .filter(people -> people.getAge()>18)
                // 找到地址为中国的
                .filter(people -> "中国".equals(people.getAddress()))
                // 找到姓刘的
                .filter(people -> people.getName().startsWith("刘"))
                // 便利集合
                .forEach(System.out::println);
    }
    /**
     * 传统的计算集合的方法
     * @param peoples 要被计算的集合
     */
    public static void tradition(List<People> peoples){
        List<People> temp1 = new ArrayList<>();
        // 第一次便利找到年龄大于18的
        for (People people : peoples) {
            if (people.getAge()>18){
                temp1.add(people);
            }
        }

        List<People> temp2 = new ArrayList<>();
        // 第二次找到中国的
        for (People people : temp1) {
            if ("中国".equals(people.getAddress())){
                temp2.add(people);
            }
        }
        List<People> temp3 = new ArrayList<>();
        // 第三次找到姓刘的
        for (People people : temp2) {
            if (people.getName().startsWith("刘")){
                temp3.add(people);
            }
        }
        System.out.println(temp3);
    }
}

@Data
@AllArgsConstructor
class People{
    private String name;
    private Integer age;
    private String address;
}
